package com.cyc.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyc.mapper.InformationMapper;
import com.cyc.mapper.UserMapper;
import com.cyc.model.Information;
import com.cyc.model.User;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private InformationMapper informationMapper;
    public User login(String username){
        if(StringUtils.isEmpty(username)){
            return null;
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        List<User> list = userMapper.selectList(wrapper);
        if(list.size()!=0){
            return list.get(0);
        }else{
            return null;
        }
    }
    //注册
    public String reg(Information information) {
        User user = new User();
        user.setUsername(information.getUsername());
        user.setPassword(information.getPassword());
        user.setPerms(information.getPerms());
        userMapper.insert(user);
        if(informationMapper.insert(information)>0){
            return "1";
        }else{
            return "2";
        }
    }
}
